T1 P0
T0 P0

M291 S5 J1 F250 L190 H450 R"Set Left Tool Temperature" R"Set temperature of the filament that was last loaded in the Left Tool" P" "
var ll = input

G10 P0 R{var.ll} S{var.ll}

if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed || !move.axes[3].homed
  G28

G29 S2     ; disable Mesh Bed Leveling
M290 R0 S0 ; reset Z axis babystepping


M116 P0 S10    ; wait for temp
M83            ; relative extruder moves
G1 E-5 F{60}*{50}  ; retract filament
T-1
T0

G90
G1 Z250 X0 U999 Y-150 F18000
M291 R"Clean the Nozzle with a metal brush" P"Click OK when ready to proceed" S3

M98 P"0:/sys/homeall.g" S1 L1 Z1

M208 Z-5 S1         ; set axis minima to -1



G90
G1 F18000 Z20 X{-155 - sensors.probes[0].offsets[0]} Y{-150 - sensors.probes[0].offsets[1]}
G30

G4 P260
var pl = move.axes[2].userPosition

G91
G1 Z5
G90

G1 F18000 X{155 - sensors.probes[0].offsets[0]} Y{-150 - sensors.probes[0].offsets[1]}
G30

G4 P260
var pr = move.axes[2].userPosition

G91
G1 Z5
G90


;================================================================================================


G1 F18000 X-155 Y-150 Z5
M291 S3 R"Finding Z - Offset" P"Press ""OK"" to save Z position" Z1
var ml = move.axes[2].userPosition

G91
G1 Z5.5 F18000
G90

G1 F18000 X155 Y-150

G91
G1 Z-5 F18000
G90

M291 S3 R"Finding Z - Offset" P"Press ""OK"" to save Z position" Z1
var mr = move.axes[2].userPosition

G91
G1 Z10
G90

G1 X-999 Y120 Z50


var dl = var.pl - var.ml
var dr = var.pr - var.mr
var dd = var.dl - var.dr


echo "calibrated for "^{var.dd}

if exists(global.xcomp)
  set global.xcomp = var.dd
else
  global xcomp = var.dd
echo >"0:/user/xcomp.g" "if exists(global.xcomp)"
echo >>"0:/user/xcomp.g" "  set global.xcomp = "^{var.dd}
echo >>"0:/user/xcomp.g" "else"
echo >>"0:/user/xcomp.g" "  global xcomp = "^{var.dd}




M208 Z0 S1         ; set axis minima
G10 P0 R0 S0
G10 P1 R0 S0
G10 P2 R0 S0
G10 P3 R0 S0